package com.lvy.hczn.transmit.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.constant.MqConstants;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.Spu;
import com.lvy.hczn.front.system.domain.business.Warehouse;
import com.lvy.hczn.front.system.service.SpuService;
import com.lvy.hczn.front.system.service.WarehouseService;
import com.lvy.hczn.mq.rocket.ParamInfo;
import com.lvy.hczn.mq.rocket.ProducerUtil;
import com.lvy.hczn.transmit.domain.ProductSupplierGoods;
import com.lvy.hczn.transmit.domain.dto.cteck.TFrmLoss;
import com.lvy.hczn.transmit.service.ProductSupplierGoodsService;
import com.lvy.hczn.transmit.service.TFrmLossService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
@Slf4j
@DS("master")
public class TFrmLossServiceImpl implements TFrmLossService {

    @Autowired
    private ProducerUtil producerUtil;

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private ProductSupplierGoodsService productSupplierGoodsService;

    /**
     * 平台推送临期商品到仓库
     *
     * @param infoList
     * @return void
     * @author yw
     * @date 2023-08-30 20:36:48
     */
    @Override
    public void syncFrmLossBatch(List<ParamInfo> infoList) {
        if (infoList == null || infoList.isEmpty()) {
            throw new UtilException("数据为空");
        }
        infoList.forEach(paramInfo -> {
            if (StrUtil.isEmpty(paramInfo.getWarehouseCode())) {
                throw new UtilException("仓库编码不能为空");
            }
            if (paramInfo.getList() == null || paramInfo.getList().isEmpty()) {
                throw new UtilException("临期商品数据列表为空");
            }

            List<TFrmLoss> tFrmLossList = JSON.parseArray(JSON.toJSONString(paramInfo.getList()), TFrmLoss.class);

            Warehouse warehouse = warehouseService.getOne(Wrappers.<Warehouse>lambdaQuery().eq(Warehouse::getWarehouseCode, paramInfo.getWarehouseCode()));
            if (warehouse == null) {
                throw new UtilException("仓库编码不存在:" + paramInfo.getWarehouseCode());
            }

            log.error("syncFrmLossBatchParam:{}", JSON.toJSONString(paramInfo));
            tFrmLossList.forEach(frmLoss -> {
                ProductSupplierGoods productSupplierGoods = productSupplierGoodsService.getOne(Wrappers.<ProductSupplierGoods>lambdaQuery().eq(ProductSupplierGoods::getProductSysCode, frmLoss.getSpuCode()));
                if (productSupplierGoods == null) {
                    throw new UtilException("仓库编码为：" + paramInfo.getWarehouseCode() + "的商品编码不存在:" + frmLoss.getSpuCode());
                }
                if (frmLoss.getStatus() == null) {
                    frmLoss.setStatus(Constants.FRM_LOSS_DATA_TYPE_ONE);
                }

                if (StrUtil.isEmpty(frmLoss.getSupplierCode())) {
                    frmLoss.setSupplierCode(productSupplierGoods.getSupplierCode());
                }

                frmLoss.setRecodeCode(frmLoss.getId());

                if (StrUtil.isEmpty(frmLoss.getDataType())) {
                    frmLoss.setDataType(Constants.WAREHOUSING_ENTRY_DATA_TYPE_FIVE);
                }

                if (StrUtil.isEmpty(frmLoss.getSpecCode())) {
                    frmLoss.setSpecCode(frmLoss.getSkuCode());
                }

                if (frmLoss.getType() == null) {
                    frmLoss.setType(Constants.FRM_LOSS_DATA_TYPE_ONE);
                }
                frmLoss.setTreated(false);
                frmLoss.setSpuId(productSupplierGoods.getId());
            });

            String topic = warehouseService.selectRegionAreaTopic(warehouse);
            String tag = warehouse.getWarehouseCode() + "_" + MqConstants.TOPIC_FRM_LOSS_TAG;
            ParamInfo info = new ParamInfo();
            info.setList(tFrmLossList);
            try {
                producerUtil.sendAsyncMsg(topic, tag, JSON.toJSONString(info).getBytes(StandardCharsets.UTF_8), IdUtil.fastSimpleUUID());
            } catch (Exception e) {
                log.error("退货单信息保存失败:{}", e);
                throw new UtilException("信息发送异常");
            }
        });
    }
}
